; menubar.asm
; Main menu bar

	.include "common.inc"
	.include "global.inc"
	.include "screen.inc"
	.include "window.inc"
	.include "execfun.inc"
	.include "drawfun.inc"
	.include "graphic.inc"
	.include "text.inc"
	.include "mouse.inc"
	.include "cursor.inc"
	.include "scancodes.inc"
	.include "timer.inc"
	.include "scrsaver.inc"
	.include "wutil.inc"
	
	.export menu_init
	.export menu_show_time
	
	.export menu_time_str
	
	.import demo
	.import picture
	.import anypic
	.import setup

BAR_HEIGHT= 16

BAR_POS_X = 0
BAR_POS_Y = 0
	
	.segment "RODATA"
	
sm_width:	.byte 12
sm_linhgt:	.byte 12
sm_txthgt:	.byte 10

sm_count:	.byte 4

sm_lo:		.byte <sm_any, <sm_pic, <sm_demo, <sm_setup
sm_hi:		.byte >sm_any, >sm_pic, >sm_demo, >sm_setup

sm_adrlo:	.byte <anypic, <picture, <demo, <setup
sm_adrhi:	.byte >anypic, >picture, >demo, >setup

sm_any:		.byte 6, $56, $09, $03, $2D, $32, $30
sm_pic:		.byte 5, $42, $0C, $09, $0E, $0B 
sm_demo:	.byte 4, $54, $05, $13, $14
sm_setup:	.byte 5, $53, $05, $14, $15, $10

; sm_exit:	.byte 4, $45, $18, $09, $14

b_chicken:	.byte %00111110, %01111110, %11100001, %11100001
			.byte %11100000, %11100001, %11100001, %01111110
			.byte %00111110, %00000000, %00000000, %11100000
			.byte %11000000, %00000000, %11000000, %11100000
			.byte %00000000, %00000000

bm_chicken:	.byte 11, 9
			.word b_chicken

tx_start:	.byte $FF
			.word bm_chicken

	.segment "DATA"

h_bar:		.byte $00
h_menu:		.byte $00
f_menu:		.byte $00
h_hour:		.byte $00
h_min:		.byte $00
h_dot:		.byte $00
tx_hour:	.byte 2, "00"
tx_min:		.byte 2, "00"
tx_dot:		.byte 1, ":"
menu_time_str:	.byte "000000"
h_dif:		.byte 0
m_dif:		.byte 0

	.segment "JMPTABLE"

execptr:	.res 2

	.segment "CODE"
	
; create menu bar
menu_init:	; create main frame
			ldx scr_dim_x
			ldy #BAR_HEIGHT
			jsr frm_create
			ldx #BAR_POS_X
			ldy #BAR_POS_Y
			jsr frm_setpos
			ldy #FFLG_PERM
			jsr frm_setflags
			jsr frm_getwin
			sta h_bar
			jsr win_realize
			lda #WSTYLE_BCKGND
			jsr win_setstyle
			lda #1					; lower case system font
			jsr win_setfont
			lda #<ex_menu_bar
			ldx #>ex_menu_bar
			jsr win_setexec
			
			; create start button			
			lda h_bar
			jsr win_create
			lda #WSTYLE_BORDER
			jsr win_setstyle
			ldx #2
			ldy #1
			jsr win_setpos
			ldx #44
			ldy #13
			jsr win_setdim
			lda #<tx_start
			ldx #>tx_start
			jsr win_settext
			lda #WTYPE_BUTTON
			jsr win_settype
			lda #<ex_start_button
			ldx #>ex_start_button
			jsr win_setexec
			; create clock windows
			lda h_bar
			jsr win_create
			sta h_hour
			lda #WSTYLE_TXLEFT+WSTYLE_BCKGND
			jsr win_setstyle
			lda scr_dim_x
			sec
			sbc #37
			tax 
			ldy #4
			jsr win_setpos
			ldx #18
			ldy #8
			jsr win_setdim
			lda #<tx_hour
			ldx #>tx_hour
			jsr win_settext

			lda h_bar
			jsr win_create
			sta h_dot
			lda #WSTYLE_TXLEFT
			jsr win_setstyle
			lda scr_dim_x
			sec
			sbc #24
			tax 
			ldy #4
			jsr win_setpos
			ldx #8
			ldy #8
			jsr win_setdim
			lda #<tx_dot
			ldx #>tx_dot
			jsr win_settext
			
			lda h_bar
			jsr win_create
			sta h_min
			lda #WSTYLE_TXRIGHT+WSTYLE_BCKGND
			jsr win_setstyle
			lda scr_dim_x
			sec
			sbc #20
			tax 
			ldy #4
			jsr win_setpos
			ldx #18
			ldy #8
			jsr win_setdim
			lda #<tx_min
			ldx #>tx_min
			jsr win_settext

			; draw window			
			lda h_bar
			jsr win_draw
			lda h_bar
			jsr win_show			; to screen

			; move mouse closer to the start button
			ldx #20
			ldy #20
			jsr move_mouse

			; set timer handler
			lda #<menu_handle_1s
			ldx #>menu_handle_1s
			jmp set_1s_fun

ex_menu_bar: ; handle key events for the menu bar
			ldy #Event::type
			lda (reg2), y
			cmp #EVT_KEY_DOWN
			bne @dodefault
			ldy #KeyEvent::scncode
			lda (reg2), y
			cmp #SCAN_CODE_CBM
			beq togglem
			jmp escalate
@dodefault:	jmp ex_window

ex_start_button:
			jsr btncheck
togglem:	lda f_menu
			beq init_menu
			lda f_menu
			jsr frm_destroy
			lda #0
			sta f_menu
			rts

ex_menu:	; handle key events & focus loss for the menu bar
			ldy #Event::type
			lda (reg2), y
			cmp #EVT_WIN_KILLFOCUS
			beq togglem
			cmp #EVT_KEY_DOWN
			bne @dodefault
			ldy #KeyEvent::scncode
			lda (reg2), y
			cmp #SCAN_CODE_CBM
			beq togglem
			jmp escalate
@dodefault:	jmp ex_window

	.segment "DATA"
	
b_i:		.res 1
b_width:	.res 1
b_height:	.res 1
b_top:		.res 1
b_left:		.res 1

	.segment "CODE"
	
; init start menu pop-up
init_menu:	jsr cur_hide
			lda sm_width			; calc window width
			asl
			asl
;			asl
			sta b_width
			lda #2
			clc
			ldx sm_count			; calc window height
@mulh:		adc sm_linhgt
			dex
			bne @mulh
			sta b_height
			tay
;			lda #BAR_POS_Y			; calc window top
;			sec
;			sbc b_height
			lda #BAR_POS_Y+BAR_HEIGHT
			sta b_top
			ldx b_width
			jsr frm_create			; setup frame window
			sta f_menu
			ldx #BAR_POS_X
			ldy b_top
			jsr frm_setpos
			ldy #FFLG_TEMP
			jsr frm_setflags
			jsr frm_getwin
			sta h_menu
			jsr win_realize
			lda #WSTYLE_BCKGND
			jsr win_setstyle
			lda #2					; 4x8 pixel font
			jsr win_setfont
			lda #<ex_menu
			ldx #>ex_menu
			jsr win_setexec
			
			; init menu buttons
			ldx #BAR_POS_X+1
			stx b_left
			ldy #1
			sty b_top
			clc
			lda b_width
			sbc #2
			sta b_width
			lda #0
			sta b_i

@bt_loop:	cmp sm_count 
			beq @bt_done
			lda h_menu
			jsr win_create
			lda b_i
			jsr win_setuser
			ldx b_left
			lda b_top
			tay
			clc
			adc sm_linhgt
			sta b_top
			jsr win_setpos
			ldx b_width
			ldy sm_txthgt
			jsr win_setdim
			ldy b_i
			lda sm_lo, y
			ldx sm_hi, y
			jsr win_settext
			lda #WTYPE_BUTTON
			jsr win_settype
			lda #WSTYLE_BCKGND+WSTYLE_TXLEFT
			jsr win_setstyle
			lda #<ex_menu_button
			ldx #>ex_menu_button
			jsr win_setexec

			inc b_i
			lda b_i
			bne @bt_loop

@bt_done:	; draw window
			lda h_menu
			jsr win_draw
			lda h_menu
			jsr win_show			; to screen
			
			jmp cur_show
			
ex_menu_button:
			jsr btncheck
			jsr win_getuser
			pha
			jsr cur_hide
			lda f_menu
			jsr frm_destroy
			lda #0
			sta f_menu
			pla
			tax
			lda sm_adrlo, x
			sta execptr
			lda sm_adrhi, x
			sta execptr+1
			jsr runexec
			jsr cur_show
			rts

runexec:	jmp (execptr)

	.segment "DATA"
	
times:		.res 3
	
	.segment "CODE"

menu_handle_1s:
			jsr get_time
			sta times	; second
			stx times+1	; minute
			sty times+2	; hour
			lda #$30	; menu_time_string := "000000"
			ldx #5
@clrtimes:	sta menu_time_str, x
			dex
			bpl @clrtimes
			ldx #5
@dec_loop:	lda times+0
			cmp #10
			bcc @noinc_sec
			sbc #10
			sta times+0
			inc menu_time_str+4
@noinc_sec:	lda times+1
			cmp #10
			bcc @noinc_min
			sbc #10
			sta times+1
			inc menu_time_str+2
@noinc_min:	lda times+2
			cmp #10
			bcc @noinc_hr
			sbc #10
			sta times+2
			inc menu_time_str+0
@noinc_hr:	dex
			bne @dec_loop
			clc
			lda times+0
			adc #$30
			sta menu_time_str+5
			lda times+1
			adc #$30
			sta menu_time_str+3
			lda times+2
			adc #$30
			sta menu_time_str+1

menu_show_time:
			; menu_time_str
			lda menu_time_str	; create display string from time string
			eor tx_hour+1
			sta h_dif
			lda menu_time_str+1
			eor tx_hour+2
			ora h_dif
			beq @do_minute
	
			lda menu_time_str	; create display string from time string
			sta tx_hour+1
			lda menu_time_str+1
			sta tx_hour+2
			
			lda h_hour
			jsr win_draw
	
@do_minute: lda menu_time_str+2
			eor tx_min+1
			sta m_dif
			lda menu_time_str+3
			eor tx_min+2
			ora m_dif
			beq @do_second

			lda menu_time_str+2
			sta tx_min+1
			lda menu_time_str+3
			sta tx_min+2

			lda h_min
			jsr win_draw

@do_second:	lda h_dot
			jsr win_select
			jsr win_begindraw
			lda #GM_XOR
			jsr g_mode
			lda #1
			jsr t_font
			lda #<tx_dot
			ldx #>tx_dot
			jsr drw_wintxt
			
			jmp do_saver_1s

